package com.glamour.hlcity.component.task.impl;

import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import com.glamour.hlcity.common.annotation.MySqlLockAspect;
import com.glamour.hlcity.common.annotation.Param;
import com.glamour.hlcity.common.model.CommandDO;
import com.glamour.hlcity.component.log.util.LogTracker;
import com.glamour.hlcity.component.task.api.CmdExecute;
import com.glamour.hlcity.component.task.model.ResultModel;
import com.glamour.hlcity.dal.lock.dao.impl.DBLock;

public abstract class AbstractCmdExecute extends AbstractCmdCreateExecute implements CmdExecute {
	protected Log LOG=LogFactory.getLog(AbstractCmdExecute.class);
	protected final static String RESULT_SUCCESS="success";
	protected final static String RESULT_FAIL="fail";
	protected DBLock dBLock;
	private final static String TABLE_NAME="glamour_command";
	@Override
	@Transactional
	@MySqlLockAspect(lockName=TABLE_NAME,params={"bizNo","type"})
	public boolean execute(@Param(name="type")String type, @Param(name="bizNo")String bizNo) {
		LOG.info(LogTracker.getLogContext().addClue("类型", type).addClue("业务编号", bizNo).getMessage("执行命令"));
		//第一步：执行该命令类型的操作
		act(bizNo);
		//第二步：结束命令
		CommandDO command=command(bizNo, type);
		cmdDone(command);
		//第三步：调用回调函数
		ResultModel result=search(command.getExtSeqno());
		callBack(bizNo, result);
		return true;
	}

	protected Integer getLock(String lockName){
		return dBLock.getLock(lockName, BigDecimal.ZERO.intValue());
	}
	
	protected Integer releaseLock(String lockName){
		return dBLock.releaseLock(lockName);
	}
	
	public void setdBLock(DBLock dBLock) {
		this.dBLock = dBLock;
	}
}
